package com.cqeec.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.cqeec.domain.Teacher;
import com.cqeec.service.TeacherService;
import com.cqeec.service.imp.TeacherServiceImpl;
import com.cqeec.utils.JDBCUtils;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
/**
 * 添加教师
 * @author Administrator
 *
 */
public class AddTeacher extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddTeacher frame = new AddTeacher();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddTeacher() {
		setIconifiable(true);
		setClosable(true);
		setTitle("添加教师");
		setBounds(100, 100, 553, 553);
		
		JLabel lblNewLabel = new JLabel("教师姓名：");
		
		JLabel lblNewLabel_1 = new JLabel("教师工号：");
		
		JLabel lblNewLabel_2 = new JLabel("登录密码：");
		
		JLabel lblNewLabel_3 = new JLabel("课程代号：");
		
		textField = new JTextField();
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Connection conn = JDBCUtils.getConnection();
				String teacherName=textField.getText(); //获取学生姓名
				String teacherNum=textField_1.getText();//获取学生学号
				String teacherPwd=textField_2.getText();//获取学生登录密码
				String teacherMajor=textField_4.getText();//获取学生院系
				String courseNum=textField_3.getText();//获取学生院系
				
				if(teacherName.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入教师姓名", "提示", JOptionPane.ERROR_MESSAGE);
					textField.requestFocus();
					return;
				}
				
				if(teacherNum.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入教师工号", "提示", JOptionPane.ERROR_MESSAGE);
					textField_1.requestFocus();
					return;
				} 

				if(teacherPwd.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入教师登录密码密码", "提示", JOptionPane.ERROR_MESSAGE);
					textField_2.requestFocus();
					return;
				}

				if(teacherMajor.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入教师院系", "提示", JOptionPane.ERROR_MESSAGE);
					textField_3.requestFocus();
					return;
				}
				if(courseNum.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入教师课程代码", "提示", JOptionPane.ERROR_MESSAGE);
					textField_3.requestFocus();
					return;
				}
				Teacher teacher=new Teacher(teacherName, teacherNum,  teacherPwd, teacherMajor, courseNum);
			    
				TeacherService service=new TeacherServiceImpl();
				int rs=service.addteachers(conn,teacher);
				if(rs!=0) {
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.ERROR_MESSAGE);
				}
				
				
			}
		});
		
		JButton button_1 = new JButton("重置");
		
		JLabel label = new JLabel("教师系别：");
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(83)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(textField_1))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(textField, GroupLayout.PREFERRED_SIZE, 256, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblNewLabel_2)
									.addGap(18)
									.addComponent(textField_2))
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addComponent(label)
										.addComponent(lblNewLabel_3))
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addGroup(groupLayout.createSequentialGroup()
											.addGap(26)
											.addComponent(textField_3))
										.addGroup(groupLayout.createSequentialGroup()
											.addGap(18)
											.addComponent(textField_4, GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE))))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(126)
							.addComponent(button)
							.addGap(91)
							.addComponent(button_1)))
					.addContainerGap(109, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(103)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(65)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(46)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(46)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(28)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(button)
						.addComponent(button_1))
					.addGap(36))
		);
		getContentPane().setLayout(groupLayout);

	}
}
